package net.esj.basic.utils.cookies;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookieUtil {

	public static void readCookie(HttpServletRequest request, CookieEntity cookieEntity){
		Cookie[] cookies = request.getCookies();
		if(cookies!=null){
			for (int i = 0; i < cookies.length; i++) {
				Cookie c = cookies[i];  
				if(c.getName().equalsIgnoreCase("YYID")){
					cookieEntity.setYyId(c.getValue());
				}
			}
		}
	}

	public static void writeCookie(HttpServletResponse response, CookieEntity cookieEntity){
		//生命周期，两周
		int times = 1000 * 60 * 60 * 24 * 14;
		writeCookie(response, cookieEntity, times);
	}
	
	public static void writeCookie(HttpServletResponse response, CookieEntity cookieEntity,int times){
		/*
		 * 写cookie
		 */
		Cookie cookie = new Cookie("YYID",cookieEntity.getYyId());
		cookie.setMaxAge(times);
		cookie.setPath("/");
		response.addCookie(cookie);
	}
}
